import { getUserMenu } from "@/api/system/user";
import { deepClone } from "@/utils/lib/clone";
import { defineStore } from "pinia";
import { RouteRecordRaw } from "vue-router";

interface IAsyncRouteState {
  keepAliveComponents: string[]
  asyncRoute:any
  isAddRoute:boolean
}

export const useAsyncRouteStore = defineStore({
  id: 'app-async-route',
  state: (): IAsyncRouteState => ({
    keepAliveComponents:[],
    asyncRoute:[],
    isAddRoute :false
  }),
  actions: {
    setKeepAliveComponents(componentName) {
      this.keepAliveComponents.push(componentName);
    },
    setAsyncRoutes(routes: RouteRecordRaw[]){
      this.asyncRoute = routes
    },
    setIsAddRoute(isAddRoute:boolean){
      this.isAddRoute = isAddRoute
    },
    async generateRoutes(user_id:string){
      try {
        const res = await getUserMenu({
          user_id
        })
        let route = deepClone(res.data.data)
        route = route.map(item => item.permission_name)
        
        
        return Promise.resolve(route)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  }
})